import React,{Component} from 'react';
import './login.less'
import {Link} from 'react-router-dom'
import { Form, Input, Row, Col ,message,Button} from 'antd';
import { UserOutlined, LockOutlined } from '@ant-design/icons';
import {registerApi} from '../../api/index.js'
import {login_info} from '../../store/action-type'
import {connect} from 'react-redux'
import { withRouter } from 'react-router-dom';
import {randomNum} from "@/utils/util"

@withRouter 
class RegisterForm extends Component {
    constructor(props){
         super(props);
         this.canvas=null;
         this.state={
            focusItem: -1,   //当前焦点聚焦在哪一项上
            code: ''  //验证码
         }
    }
    onFinish= async (val)=>{
        const parmas={
          user_name:val.username,
          user_pwd:val.password
        }
    
        await registerApi(parmas).then(res=>{
        console.log(res)
            if(res.status==0){
              message.success(res.msg?res.msg:"注册成功")
              // this.props.history.push('/home')
              return
            }
            message.error(res.msg?res.msg:"注册失败！")
        }).catch(res=>{

        })
    }
    loginHandle(){
      // 切换
      this.props.toggleShow()
    }
    componentDidMount(){
      this._createCode()
    }
    _createCode(){
      const ctx = this.canvas.getContext('2d')
      const chars = [1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
      let code = ''
      ctx.clearRect(0, 0, 80, 40) //clear 画布
      for (let i = 0; i < 4; i++) {
        const char = chars[randomNum(0, 57)]
        code += char
        ctx.font = randomNum(20, 25) + 'px SimHei'  //设置字体随机大小
        ctx.fillStyle = '#D3D7F7'
        ctx.shadowOffsetX = randomNum(-3, 3)
        ctx.shadowOffsetY = randomNum(-3, 3)
        ctx.shadowBlur = randomNum(-3, 3)
        ctx.shadowColor = 'rgba(0, 0, 0, 0.5)'
        let x = 80 / 5 * (i + 1)
        let y = 40 / 2
        let deg = randomNum(-25, 25)
        /**设置旋转角度和坐标原点**/
        ctx.translate(x, y)
        ctx.rotate(deg * Math.PI / 180)
        ctx.fillText(char, 0, 0)
        /**恢复旋转角度和坐标原点**/
        ctx.rotate(-deg * Math.PI / 180)
        ctx.translate(-x, -y)
      }
      this.setState({
        code
      })

    }
    render(){
        const { focusItem, code } = this.state;
        console.log(this.props)
        return(
            <div className="login-contianer">
                <h3 className="title">管理员注册</h3>
                <Form hideRequiredMark  onFinish={this.onFinish.bind(this)}>
                    <Form.Item
                       style={{ marginBottom: 10 }}
                       colon={false}
                       labelCol={{ span: 3, pull: focusItem === 0 ? 1 : 0 }}
                       label={<span className='iconfont icon-User' style={{ opacity: focusItem === 0 ? 1 : 0.6 }} />}
                       wrapperCol={{ span: 20, pull: focusItem === 0 ? 1 : 0 }}
                       name="username"
                       rules={ [
                            { required: true, message: '请输入用户名' },
                            { pattern: /^[^\s']+$/, message: '不能输入特殊字符' },
                        ]}
                    >
                        <Input 
                            className="myInput"
                            placeholder="用户名"
                            onFocus={() => this.setState({ focusItem: 0 })}
                            onBlur={() => this.setState({ focusItem: -1 })}
                            onPressEnter={this.submit}
                        />
                    </Form.Item>
                    <Form.Item
                     style={{ marginBottom: 10 }}
                     wrapperCol={{ span: 20, pull: focusItem === 1 ? 1 : 0 }}
                     labelCol={{ span: 3, pull: focusItem === 1 ? 1 : 0 }}
                     label={<span className='iconfont icon-suo1' style={{ opacity: focusItem === 1 ? 1 : 0.6 }} />}
                     colon={false}
                     name="password"
                       rules={ [
                            { required: true, message: '请输入密码' },
                        ]}
                    >
                        <Input 
                          className="myInput"
                          type="password"
                          placeholder="密码"
                          onFocus={() => this.setState({ focusItem: 1 })}
                          onBlur={() => this.setState({ focusItem: -1 })}
                          onPressEnter={this.submit}
                       />
                    </Form.Item>
                    <Form.Item
                      style={{ marginBottom: 20 }}
                      wrapperCol={{ span: 20, pull: focusItem === 2 ? 1 : 0 }}
                      labelCol={{ span: 3, pull: focusItem === 2 ? 1 : 0 }}
                      label={<span className='iconfont icon-securityCode-b' style={{ opacity: focusItem === 2 ? 1 : 0.6 }} />}
                      colon={false}
                    >
                        <Row gutter={8}>
                            <Col span={15}>
                                <Input 
                                  className="myInput"
                                  placeholder="验证码"
                                  onFocus={() => this.setState({ focusItem: 2 })}
                                  onBlur={() => this.setState({ focusItem: -1 })}
                                  onPressEnter={this.submit}
                               />
                            </Col>
                            <Col span={9}>
                                <canvas  width="80" height='40' ref={el => this.canvas = el} />
                            </Col>
                        </Row>
                    </Form.Item>
                    <Form.Item>
                        <div className="btn-box">
                            <div    className="registerBtn" onClick={this.loginHandle.bind(this)} >登录</div>
                            <Button className="loginBtn"  htmlType="submit" >注册</Button>
                        </div>
                    </Form.Item>
                </Form>
                <div className="footer">欢迎登陆后台管理系统</div>
            </div>
        )
    }
}


export default RegisterForm;